package com.hgk.common.common.ui.fragment;

import com.common.android.volley.VolleyError;
import com.common.android.volley.toolbox.RequestListener;
import com.common.android.volley.toolbox.VolleyManager;
import com.common.dongqiuhui.apps.comm.model.BaseController;

import java.util.Map;

/**
 * Created by sam on 15/8/6.
 */
public abstract class SimpleBaseFragment extends TitleFragment  {


    /**
     * 加载数据
     */
    protected void getNetDataForPost(final String action,String url,Class classOfT){

        VolleyManager.getInstance().post(url, getApiParams(), new RequestListener() {
            @Override
            public void requestSuccess(Object result) {
                BaseController baseController;
                if(result instanceof BaseController){
                    baseController= (BaseController) result;
                    if("10000".equals(baseController.getCode())){
                        success(action, result);
                    }else{
                        fail(action,baseController.getMsg());
                    }
                }
            }

            @Override
            public void requestError(VolleyError e) {
                netfail(action,e);
            }
        }, classOfT);
    }

    protected void getNetDateForGet(final String action,String url,Class classOfT){
        VolleyManager.getInstance().get(url, getApiParams(), new RequestListener() {
            @Override
            public void requestSuccess(Object result) {
                BaseController baseController;
                if (result instanceof BaseController) {
                    baseController = (BaseController) result;
                    if ("10000".equals(baseController.getCode())) {
                        success(action, result);
                    } else {
                        fail(action, baseController.getMsg());
                    }
                }
            }

            @Override
            public void requestError(VolleyError e) {
                fail(action, e.getMessage());
            }
        }, classOfT);
    }
    /**
     * Api参数
     */
    protected abstract Map getApiParams();

    /**
     * 成功
     */
    protected  abstract void success(String action,Object result);

    protected  abstract void fail(String action,String errorStr);

    protected  abstract void netfail(String action,VolleyError volleyError);

}
